package com.yuchen.advsdk.util;


import android.os.Build;
import android.text.TextUtils;
import android.util.Base64;

public class AuthImpl {
    public int auth() {
        String deviceSeed = readSeed();
        if (TextUtils.isEmpty(deviceSeed)) {
            return 3;
        }
        deviceSeed = deviceSeed.trim();
        String license = FileUtil.readFile("/storage/emulated/0/license.txt");
        if (TextUtils.isEmpty(license)) {
            return 1;
        }
        String[] split = license.split("-END-");
        String key = split[0];
        String text = split[1];
        String uuidAndSeed = RSAUtil.decryptByPrivateKey(text, key);
        String uuid = uuidAndSeed.substring(0, 50);
        String seed = uuidAndSeed.substring(50, uuidAndSeed.length());
        seed = seed.trim();
        if (deviceSeed.equals(seed)) {
            return 0;
        } else {
            return 2;
        }
    }
    private String readSeed() {
        String mac = DeviceUtil.getMac();
        if ("default_mac".equalsIgnoreCase(mac)) {
            mac = "none";
        }
        String sn = Build.SERIAL;
        String seed = mac + "*" + sn;
        return new String(Base64.encode(seed.getBytes(), Base64.DEFAULT));
    }
    public String seed() {
        return readSeed();
    }
}
